#!/usr/bin/env python
# -*- coding: utf-8 -*-
#

from celtuce import KEY_MIN, KEY_MAX
from celtuce.memstorer import Storer

import os,os.path,tempfile,time

import celtuce.help.logging as logging


PATH = os.sep.join((tempfile.gettempdir(), 'celtuce/hello'))

TABLE = 'hellotbl'


class   TestStorer_Hello(object) :
    def setUp(self) :
        logger = logging.getLogger()
        #logger.setLevel(logging.LEVEL_DEBUG)

        if os.path.exists(PATH) :
           self.removedirs(PATH)

        os.makedirs(PATH, 0777)

        self.storer = Storer()
        self.storer.startup( PATH )

        self.storer.setCellVolume( 256 * 1024 * 1024 )

        response = self.storer.createTablet( TABLE, KEY_MIN, KEY_MAX )
        if not response.isOk() :
            self.storer.shutdown()
            print response.status_code,response.error
            raise RuntimeError, response.error

        ## setUp()

    def tearDown(self) :
        self.storer.shutdown()
        ## tearDown()

    def removedirs(self, path) :
        items = os.listdir(path)
        for name in items :
            fullpath = os.sep.join((path, name))
            if os.path.isdir(fullpath) :
                self.removedirs(fullpath)
            else :
                os.remove(fullpath)

        os.rmdir(path)
        ## removedirs

    def run(self) :
        self.testOne()
        self.testTwo()
        ## run()

    def testOne(self) :
        key = 'hello'
        value = 'world'
        response = self.storer.insert( TABLE, key, value, 12345678 )
        if not response.isOk() :
           raise RuntimeError, response.error
        
        response = self.storer.find( TABLE, key )
        if not response.isOk() :
           raise RuntimeError, response.error
        print repr(response)
        print response.body
        assert response.body[3] == value
        ## testOne()

    def testTwo(self) :
        RECORD_COUNT = 10000
        key_prefix = 'hello'
        value_prefix = 'world'
        time_start = time.time()
        count = 0
        for i in range(RECORD_COUNT) :
            key = '-'.join((key_prefix, str(i)))
            value = '-'.join((value_prefix, str(i)))
            response = self.storer.insert( TABLE, key, value, 12345678 )
            if not response.isOk() :
               raise RuntimeError, response.error
            count += 1
        time_last = time.time() - time_start
        tps = count / time_last
        print ' - insert',count,'records last','%.2f' % time_last,'seconds, tps=','%.2f' % tps

        time_start = time.time()
        count = 0
        for i in range(RECORD_COUNT) :
            key = '-'.join((key_prefix, str(i)))
            value = '-'.join((value_prefix, str(i)))
            response = self.storer.find( TABLE, key )
            if not response.isOk() :
               raise RuntimeError, response.error
            #print repr(response)
            #print response.body
            assert response.body[3] == value
            count += 1
        time_last = time.time() - time_start
        tps = count / time_last
        print ' - find',count,'records last','%.2f' % time_last,'seconds, tps=','%.2f' % tps
        ## testTwo()

    ## class TestStorer_Hello


if  __name__ == '__main__' :
    t = TestStorer_Hello()
    t.setUp()
    try :
        t.run()
    finally :
        t.tearDown()


